package cn.tx.mq.listener;

import cn.tx.constants.HotelMqConstants;
import cn.tx.hotel.service.IHotelService;
import cn.tx.pojo.domain.Hotel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author: 监听MQ消息
 * @create: 2022-12-06 15:28
 * @Description:
 */
@Component
public class HotelListener {

    private static final Logger logger = LoggerFactory.getLogger(HotelListener.class);

    @Autowired
    private IHotelService hotelService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = HotelMqConstants.HOTEL_INSERT_QUEUE_NAME),
            exchange = @Exchange(name = HotelMqConstants.HOTEL_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = HotelMqConstants.HOTEL_INSERT_KEY
    ))
    public void listenHotelInsert(Long hotelId){
        logger.info("UserApplication-MQListener-listenHotelInsert");
        // 新增
        Hotel hotel = new Hotel();
        hotel.setId(hotelId);
        hotelService.save(hotel);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = HotelMqConstants.HOTEL_DELETE_QUEUE_NAME),
            exchange = @Exchange(name = HotelMqConstants.HOTEL_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = HotelMqConstants.HOTEL_DELETE_KEY
    ))
    public void listenHotelDelete(Long hotelId){
        logger.info("UserApplication-MQListener-listenHotelDelete");
        // 删除
        hotelService.removeById(hotelId);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = HotelMqConstants.HOTEL_QUERY_QUEUE_NAME),
            exchange = @Exchange(name = HotelMqConstants.HOTEL_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = HotelMqConstants.HOTEL_QUERY_KEY
    ))
    public void listenHotelQuery(Map<String, Object> params){
        logger.info("UserApplication-MQListener-listenHotelQuery");
        // 查询
        hotelService.queryList(params);
    }

}
